package com.rank.list.controller;

import cn.hutool.core.util.ReflectUtil;
import com.rank.list.service.TestReflectService;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
public class ListController {
  public static final String LIST_RANK = "list_rank";
  @Autowired private RedisTemplate<String, Object> redisTemplate;

  @Autowired private TestReflectService testReflectService;

  @GetMapping("/insert")
  public void insert() {
    ArrayList<User> userArrayList =
        Lists.newArrayList(new User("唐海岚", "男"), new User("刘亦菲", "女"), new User("哈哈哈", "无"));
    for (User user : userArrayList) {
      redisTemplate.opsForList().leftPush(LIST_RANK, user);
    }
    // redisTemplate.opsForList().leftPushAll(LIST_RANK, userArrayList);

  }

  @GetMapping("/testReflectUtil")
  public void testReflectUtil() {
    List<String> ids = new ArrayList<>();
    ReflectUtil.invoke(testReflectService, "exec", ids);
  }

  @GetMapping("/select")
  public ResponseEntity select() {
    // 查询
    return ResponseEntity.ok(
        Objects.requireNonNull(redisTemplate.opsForList().range(LIST_RANK, 0, 1)));
  }

  @GetMapping("/delete")
  public void delete() {}

  @GetMapping("/update")
  public void update() {}

  public static class User implements Serializable {
    public User() {}

    private String name;
    private String sex;

    public User(String name, String sex) {
      this.name = name;
      this.sex = sex;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getSex() {
      return sex;
    }

    public void setSex(String sex) {
      this.sex = sex;
    }
  }
}
